import React from 'react';
import { notification } from 'antd';
import { exec } from 'child_process';

window.addEventListener('error', function (e) {
    // e instanceof ErrorEvent
    if (window.Isoption) {
        notification.error({
            message: `请求错误 ${e.message}`,
            description: e.message,
        });
    }
});
class ErrorBoundary extends React.Component {
    constructor(props) {
        super(props);
        const { currentRouter } = this.props;
        this.state = { error: null, haserror: false, errorInfo: null, currentPage: currentRouter };
    }


    static getDerivedStateFromProps(nextProps, prevState) {

        if (nextProps.currentRouter !== prevState.currentPage) {
            return {
                error: null,
                haserror: false,
                errorInfo: null,
                currentPage: nextProps.currentRouter
            };
        }
        return null;
    }
    static getDerivedStateFromError(error) {
        return { error: error, haserror: true };
    }

    componentDidCatch(error, errorInfo) {
        console.log(errorInfo)
    }



    render() {
        if (this.state.error && !window.Isoption) {
            return (
                <div>
                    <h2>出错了.</h2>
                    {this.state.error && this.state.error.toString()}
                </div>
            );
        } else {
            return this.props.children;
        }

    }
}
export default ErrorBoundary